//
//  BaseViewController.swift
//  CarApprove
//
//  Created by Nathaniel on 2017/4/5.
//  Copyright © 2017年 jiurongwang. All rights reserved.
//  dd

import UIKit
import RxSwift
import RxCocoa

class CBBaseViewController: UIViewController {
    //回收袋
    let disposeBag = DisposeBag()
    
    //Navigation回退页面数量
    var backLevel:Int = 1
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func setUpNav() {
        //添加背景
        self.navigationController?.navigationBar.setBackgroundImage(UIImage.createImage(by: UIColor(hex:0x3276e6), size: CGSize(width: Screen.width, height: NAV_HT)), for: UIBarMetrics.default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.isTranslucent = false
        self.navigationController?.navigationBar.tintColor = .white
        //导航栏字体
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white, NSAttributedStringKey.font:UIFont.systemFont(ofSize: 18)]
    }
    
    func setupLeftBtnAction() {
        if let cnt = (self.navigationController?.viewControllers.count) {
            if cnt > 1 {
                //导航栏左按钮和字体属性
                let leftBarButtonItem:UIBarButtonItem = UIBarButtonItem(image: UIImage(named: "return") , style: UIBarButtonItemStyle.plain, target: self, action: #selector(backAction))
                leftBarButtonItem.tintColor = .white
                self.navigationItem.leftBarButtonItem = leftBarButtonItem
                self.hidesBottomBarWhenPushed = true
            }
        }
    }
    
    @objc func backAction() {
        if backLevel == -1 {  //默认backLevel = 1
            self.navigationController?.popToRootViewController(animated: false)
        } else {
            let vcIndex = self.navigationController?.viewControllers.index(of: self) ?? 0
            var toLevel = vcIndex - backLevel
            if toLevel < 0 {
                toLevel = 0
            }
            self.navigationController?.popToViewController((self.navigationController?.viewControllers[toLevel])!, animated: true)
        }
    }
    
    func setupRightBtnAction() {
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        setUpNav()
        setupLeftBtnAction()
        setupRightBtnAction()
    }

}
